package com.fang.transfer.file.service;

import com.fang.transfer.file.common.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.LinkedHashMap;

@Service
@Slf4j
public class UserService {

    private LinkedHashMap<String, User> userMap = new LinkedHashMap();

    public void addUser(User user) {
        userMap.put(user.getId(), user);
    }

    public User getById(String id) {
        User user = userMap.get(id);
        if (user == null) {
            throw new RuntimeException("所选用户不在线");
        }
        return user;
    }

    public Collection<User> getAllUser() {
        return userMap.values();
    }

    public synchronized void logout(String id) {
        userMap.remove(id);
    }

}
